package com.lingshi.shopping_goods_service.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lingshi.shopping.entity.Goods;
import com.lingshi.shopping.entity.ProductType;
import com.lingshi.shopping.enums.ErrorEnums;
import com.lingshi.shopping.exception.BusException;
import com.lingshi.shopping.service.ProductTypeService;
import com.lingshi.shopping_goods_service.mapper.GoodsMapper;
import com.lingshi.shopping_goods_service.mapper.ProductTypeMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

@DubboService
public class ProductTypeServiceImpl implements ProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void add(ProductType productType) {
        //默认一级分类
        productType.setLevel(1);
        /**
         * 根据提交数据parentId判断当前 父分类是几级，最多3级
         */
        LambdaQueryWrapper<ProductType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductType::getId, productType.getParentId());
        ProductType parent = productTypeMapper.selectOne(queryWrapper);

        if (Objects.nonNull(parent)) {
            if (parent.getLevel() < 3) {
                productType.setLevel(parent.getLevel() + 1);
            } else {
                throw new BusException(ErrorEnums.INSERT_OR_UPDATE_PRODUCT_TYPE_ERROR);
            }
        }

        productTypeMapper.insert(productType);

    }

    @Override
    public void update(ProductType productType) {
        //默认一级分类
        productType.setLevel(1);
        /**
         * 根据提交数据parentId判断当前 父分类是几级，最多3级
         */
        LambdaQueryWrapper<ProductType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductType::getId, productType.getParentId());
        ProductType parent = productTypeMapper.selectOne(queryWrapper);

        if (Objects.nonNull(parent)) {
            if (parent.getLevel() < 3) {
                productType.setLevel(parent.getLevel() + 1);
            } else {
                throw new BusException(ErrorEnums.INSERT_OR_UPDATE_PRODUCT_TYPE_ERROR);
            }
        }

        productTypeMapper.updateById(productType);

    }

    @Override
    public ProductType findById(Long id) {
        return productTypeMapper.selectById(id);
    }

    @Override
    public void delete(Long id) {
        /**
         * 1，判断要是删除的分类是否有字字分类
         * 2，判断要删除的分类是否还有商品关联
         */
        LambdaQueryWrapper<ProductType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductType::getParentId, id);
        Long count = productTypeMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new BusException(ErrorEnums.PRODUCT_TYPE_HAS_CHILDREN);
        }

        LambdaQueryWrapper<Goods> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Goods::getProductType1Id, id)
                .or().eq(Goods::getProductType2Id, id)
                .or().eq(Goods::getProductType3Id, id);
        Long goodsCount = goodsMapper.selectCount(queryWrapper1);
        if (goodsCount > 0) {
            throw new BusException(ErrorEnums.PRODUCT_TYPE_HAS_GOODS);
        }


        productTypeMapper.deleteById(id);

    }

    @Override
    public Page<ProductType> search(String name, int page, int size) {
        LambdaQueryWrapper<ProductType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductType::getLevel, 1);
//        if(StrUtil.isNotBlank(name)){
//            queryWrapper.like(ProductType::getName, name);
//        }
        queryWrapper.like(StrUtil.isNotBlank(name), ProductType::getName, name);

        Page<ProductType> objectPage = new Page<>(page, size);

        productTypeMapper.selectPage(objectPage, queryWrapper);

        return objectPage;
    }

    @Override
    public List<ProductType> findProductType(ProductType productType) {
        LambdaQueryWrapper<ProductType> queryWrapper = new LambdaQueryWrapper<>();
        if (Objects.nonNull(productType) && StrUtil.isNotBlank(productType.getName())) {
           queryWrapper.like(ProductType::getName, productType.getName());
        }

        return productTypeMapper.selectList(queryWrapper);
    }

    @Override
    public List<ProductType> findByParentId(Long parentId) {

        LambdaQueryWrapper<ProductType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductType::getParentId, parentId);

        return productTypeMapper.selectList(queryWrapper);
    }
}
